# -*- coding:utf-8 -*-
import json
import subprocess
import sys
 
# 需要时间变量参数
def generate_config(dt):
    config = {
        "job": {
            "setting": {
                "speed": {
                    "channel": 1
                }
            },
            "content": [
                {
                    "reader": {
                        "name": "hdfsreader",
                        "parameter": {
                            "path": "/user/hive/warehouse/hdp_lbg_supin_zplisting.db/app_hism_movielens_movie_report/dt="+dt,
                            "defaultFS": "hdfs://node101:9090",
                            "fileType": "text",
                            "encoding": "UTF-8",
                            "fieldDelimiter": "\u0001",
                            "column": [
                                {
                                    "index": 0,
                                    "type": "string"
                                },{
                                    "index": 0,
                                    "type": "string"
                                },
                                {
                                    "index": 1,
                                    "type": "string"
                                }
                            ]
                        }
                    },
                    "writer": {
                        "name": "mysqlwriter",
                        "parameter": {
                            "writeMode": "insert",
                            "username": "zpbi_wr",
                            "password": "39086120578ddd85",
                            "column": [
                                "report_date",
                                "genres_cnt",
                                "dist_cnt"
                            ],
                            "connection": [
                                {
                                    "jdbcUrl": "jdbc:mysql://hllxd101:53149/db58_zp_bi_dev",
                                    "table": ["app_hism_movielens_movie_report"]
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }
    return config
 
if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python datax_runner.py <dt>")
        sys.exit(1)

    dt = sys.argv[1]
    config = generate_config(dt)
    with open("datax_config.json", "w") as f:
        json.dump(config, f)
    
    try:
        subprocess.run(["python", "datax.py", "datax_config.json"], check=True)
        print("DataX job completed successfully.")
    except subprocess.CalledProcessError as e:
        print("DataX job failed with error code {e.returncode}")
